<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="custom-style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
	<div style="overflow: hidden;">
		<div style="height: 57px;min-width: 1039px;background-size: auto 100%;background:url('');">
			<img src="titleleft.png" style="float: left;"/>
			<img src="titleright.png" alt="" style="float: right;" />
		</div>
	</div>
<!-- 制作者 Doxygen 1.8.15 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>法律声明</span></a></li>
      <li><a href="_xE9_xA6_x96_xE9_xA1_xB5.html"><span>首页</span></a></li>
      <li><a href="_xE6_x9B_xB4_xE6_x96_xB0_xE8_xAE_xB0_xE5_xBD_x95.html"><span>更新记录</span></a></li>
      <li><a href="_xE7_x8E_xAF_xE5_xA2_x83_xE9_x85_x8D_xE7_xBD_xAE.html"><span>环境配置</span></a></li>
      <li><a href="_xE7_xBC_x96_xE7_xA8_x8B_xE5_xBC_x95_xE5_xAF_xBC.html"><span>编程引导</span></a></li>
      <li><a href="modules.html"><span>接口说明</span></a></li>
      <li><a href="annotated.html"><span>结构体定义</span></a></li>
      <li><a href="_xE7_x9B_xB8_xE6_x9C_xBA_xE5_x8F_x82_xE6_x95_xB0_xE8_x8A_x82_xE7_x82_xB9_xE8_xA1_xA8.html"><span>相机参数节点表</span></a></li>
      <li><a href="examples.html"><span>示例程序</span></a></li>
      <li><a href="_xE9_x94_x99_xE8_xAF_xAF_xE7_xA0_x81.html"><span>状态码</span></a></li>
      <li><a href="_xE5_xB8_xB8_xE8_xA7_x81_xE9_x97_xAE_xE9_xA2_x98.html"><span>常见问题</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">ConnectSpecCamera.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>通过IP地址连接相机</p>
<p>该示例程序说明如何通过IP地址连接网口相机。通过输入需要连接的相机IP（Camera Ip）和相机对应的网卡IP（Export Ip）来连接相机，输入的格式为xx.xx.xx.xx。</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;pthread.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;MvCameraControl.h&quot;</span></div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> g_bExit = <span class="keyword">false</span>;</div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> g_nPayloadSize = 0;</div><div class="line"></div><div class="line"><span class="comment">// 等待用户输入enter键来结束取流或结束程序</span></div><div class="line"><span class="comment">// wait for user to input enter to stop grabbing or end the sample program</span></div><div class="line"><span class="keywordtype">void</span> PressEnterToExit(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> c;</div><div class="line">    <span class="keywordflow">while</span> ( (c = getchar()) != <span class="charliteral">&#39;\n&#39;</span> &amp;&amp; c != EOF );</div><div class="line">    fprintf( stderr, <span class="stringliteral">&quot;\nPress enter to exit.\n&quot;</span>);</div><div class="line">    <span class="keywordflow">while</span>( getchar() != <span class="charliteral">&#39;\n&#39;</span>);</div><div class="line">    g_bExit = <span class="keyword">true</span>;</div><div class="line">    sleep(1);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span>* WorkThread(<span class="keywordtype">void</span>* pUser)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> nRet = MV_OK;</div><div class="line"></div><div class="line">    <a name="_a0"></a><a class="code" href="struct_m_v___f_r_a_m_e___o_u_t___i_n_f_o___e_x.html">MV_FRAME_OUT_INFO_EX</a> stImageInfo = {0};</div><div class="line">    memset(&amp;stImageInfo, 0, <span class="keyword">sizeof</span>(<a class="code" href="struct_m_v___f_r_a_m_e___o_u_t___i_n_f_o___e_x.html">MV_FRAME_OUT_INFO_EX</a>));</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * pData = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>) * (g_nPayloadSize));</div><div class="line">    <span class="keywordflow">if</span> (NULL == pData)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> NULL;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nDataSize = g_nPayloadSize;</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(1)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span>(g_bExit)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        nRet = <a name="a1"></a><a class="code" href="group___xE5_x9B_xBE_xE5_x83_x8F_xE9_x87_x87_xE9_x9B_x86.html#gafb284e0ae3eeced7e1f9395317800c6f">MV_CC_GetOneFrameTimeout</a>(pUser, pData, nDataSize, &amp;stImageInfo, 1000);</div><div class="line">        <span class="keywordflow">if</span> (nRet == MV_OK)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n&quot;</span>, </div><div class="line">                stImageInfo.<a name="a2"></a><a class="code" href="struct_m_v___f_r_a_m_e___o_u_t___i_n_f_o___e_x.html#a523c75b72da2a3bb32b97032f86c4703">nWidth</a>, stImageInfo.<a name="a3"></a><a class="code" href="struct_m_v___f_r_a_m_e___o_u_t___i_n_f_o___e_x.html#a33071a62b54a3c9a671a0fcbe4804f1a">nHeight</a>, stImageInfo.<a name="a4"></a><a class="code" href="struct_m_v___f_r_a_m_e___o_u_t___i_n_f_o___e_x.html#a6f8d00948b2d76ac8cd65fda0acc2ed6">nFrameNum</a>);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;No data[0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;        </div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    free(pData);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> nRet = MV_OK;</div><div class="line">    <span class="keywordtype">void</span>* handle = NULL;</div><div class="line">    <a name="_a5"></a><a class="code" href="struct_m_v___c_c___d_e_v_i_c_e___i_n_f_o.html">MV_CC_DEVICE_INFO</a> stDevInfo = {0};</div><div class="line">    <a name="_a6"></a><a class="code" href="struct_m_v___g_i_g_e___d_e_v_i_c_e___i_n_f_o.html">MV_GIGE_DEVICE_INFO</a> stGigEDev = {0};</div><div class="line"></div><div class="line">    <span class="comment">// ch:需要连接的相机ip(根据实际填充) | en:The camera IP that needs to be connected (based on actual padding)</span></div><div class="line">    printf(<span class="stringliteral">&quot;Please input Current Camera Ip : &quot;</span>);</div><div class="line">    <span class="keywordtype">char</span> nCurrentIp[128];</div><div class="line">    scanf(<span class="stringliteral">&quot;%s&quot;</span>, (<span class="keywordtype">char</span>*)&amp;nCurrentIp);</div><div class="line">    <span class="comment">// ch:相机对应的网卡ip(根据实际填充) | en:The pc IP that needs to be connected (based on actual padding)</span></div><div class="line">    printf(<span class="stringliteral">&quot;Please input Net Export Ip : &quot;</span>);</div><div class="line">    <span class="keywordtype">char</span> nNetExport[128];</div><div class="line">    scanf(<span class="stringliteral">&quot;%s&quot;</span>, (<span class="keywordtype">char</span>*)&amp;nNetExport);</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nIp1, nIp2, nIp3, nIp4, nIp;</div><div class="line"></div><div class="line">    sscanf(nCurrentIp, <span class="stringliteral">&quot;%d.%d.%d.%d&quot;</span>, &amp;nIp1, &amp;nIp2, &amp;nIp3, &amp;nIp4);</div><div class="line">    nIp = (nIp1 &lt;&lt; 24) | (nIp2 &lt;&lt; 16) | (nIp3 &lt;&lt; 8) | nIp4;</div><div class="line">    stGigEDev.<a name="a7"></a><a class="code" href="struct_m_v___g_i_g_e___d_e_v_i_c_e___i_n_f_o.html#aea1e6f183f4d5cf372af1add35691563">nCurrentIp</a> = nIp;</div><div class="line"></div><div class="line">    sscanf(nNetExport, <span class="stringliteral">&quot;%d.%d.%d.%d&quot;</span>, &amp;nIp1, &amp;nIp2, &amp;nIp3, &amp;nIp4);</div><div class="line">    nIp = (nIp1 &lt;&lt; 24) | (nIp2 &lt;&lt; 16) | (nIp3 &lt;&lt; 8) | nIp4;</div><div class="line">    stGigEDev.<a name="a8"></a><a class="code" href="struct_m_v___g_i_g_e___d_e_v_i_c_e___i_n_f_o.html#a313675037bf453d5b0737d6bb9b21000">nNetExport</a> = nIp;</div><div class="line"></div><div class="line">    stDevInfo.<a name="a9"></a><a class="code" href="struct_m_v___c_c___d_e_v_i_c_e___i_n_f_o.html#a3b26b1c9681dba57af81bdb7ed795575">nTLayerType</a> = <a name="a10"></a><a class="code" href="group___xE5_xAE_x8F_xE5_xAE_x9A_xE4_xB9_x89.html#gae1780ad0d41bcd2e835ab81155b398e6">MV_GIGE_DEVICE</a>;<span class="comment">// ch:仅支持GigE相机 | en:Only support GigE camera</span></div><div class="line">    stDevInfo.<a name="a11"></a><a class="code" href="struct_m_v___c_c___d_e_v_i_c_e___i_n_f_o.html#ad8047fbdb096b8801b869f00db151c69">SpecialInfo</a>.<a name="a12"></a><a class="code" href="struct_m_v___c_c___d_e_v_i_c_e___i_n_f_o.html#aab5fcc9de5a7fa798f170a8453818cc5">stGigEInfo</a> = stGigEDev;</div><div class="line"></div><div class="line">    <span class="keywordflow">do</span> </div><div class="line">    {</div><div class="line">        <span class="comment">// ch:初始化SDK | en:Initialize SDK</span></div><div class="line">        nRet = <a name="a13"></a><a class="code" href="group___s_d_k_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#ga768544eb6e6435313dd99ee9cf92d59d">MV_CC_Initialize</a>();</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Initialize SDK fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:选择设备并创建句柄 | en:Select device and create handle</span></div><div class="line">        nRet = <a name="a14"></a><a class="code" href="group___xE7_x9B_xB8_xE6_x9C_xBA_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#gaf10f0644caa6d4dd39fd8c14f8736153">MV_CC_CreateHandle</a>(&amp;handle, &amp;stDevInfo);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Create Handle fail! nRet[0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:打开设备 | en:Open device</span></div><div class="line">        nRet = <a name="a15"></a><a class="code" href="group___xE7_x9B_xB8_xE6_x9C_xBA_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#ga308c9a9869be7a457d27aadd94ce57dd">MV_CC_OpenDevice</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Open Device fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)</span></div><div class="line">        <span class="keywordtype">int</span> nPacketSize = <a name="a16"></a><a class="code" href="group___gig_e_xE7_x9B_xB8_xE6_x9C_xBA.html#ga9df4e6a7acbd6e6a6952e01949efc18b">MV_CC_GetOptimalPacketSize</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (nPacketSize &gt; 0)</div><div class="line">        {</div><div class="line">            nRet = <a name="a17"></a><a class="code" href="group___xE5_xBA_x9F_xE5_xBC_x83_xE6_x8E_xA5_xE5_x8F_xA3.html#ga6323cf15f3319cb2b23646c1ca03c166">MV_CC_SetIntValue</a>(handle,<span class="stringliteral">&quot;GevSCPSPacketSize&quot;</span>,nPacketSize);</div><div class="line">            <span class="keywordflow">if</span>(nRet != MV_OK)</div><div class="line">            {</div><div class="line">                printf(<span class="stringliteral">&quot;Warning: Set Packet Size fail nRet [0x%x]!\n&quot;</span>, nRet);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Warning: Get Packet Size fail nRet [0x%x]!\n&quot;</span>, nPacketSize);</div><div class="line">        }</div><div class="line"></div><div class="line">        </div><div class="line">        <span class="comment">// ch:设置触发模式为off | en:Set trigger mode as off</span></div><div class="line">        nRet = <a name="a18"></a><a class="code" href="group___xE5_xB1_x9E_xE6_x80_xA7_xE9_x85_x8D_xE7_xBD_xAE.html#ga15a8abe28a1c5a53e10d115c7def4089">MV_CC_SetEnumValue</a>(handle, <span class="stringliteral">&quot;TriggerMode&quot;</span>, <a name="a19"></a><a class="code" href="group___xE7_xB1_xBB_xE5_x9E_x8B_xE5_xAE_x9A_xE4_xB9_x89.html#gga0c8acd46a8855d93c57b0921b4f9bde9a673e3eb298399f4f80866b97e8f2704c">MV_TRIGGER_MODE_OFF</a>);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Set Trigger Mode fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:获取数据包大小 | en:Get payload size</span></div><div class="line">        <a name="_a20"></a><a class="code" href="struct_m_v_c_c___i_n_t_v_a_l_u_e.html">MVCC_INTVALUE</a> stParam;</div><div class="line">        memset(&amp;stParam, 0, <span class="keyword">sizeof</span>(<a class="code" href="struct_m_v_c_c___i_n_t_v_a_l_u_e.html">MVCC_INTVALUE</a>));</div><div class="line">        nRet = <a name="a21"></a><a class="code" href="group___xE5_xBA_x9F_xE5_xBC_x83_xE6_x8E_xA5_xE5_x8F_xA3.html#ga4c7fc34b34bb73d58076f7973c8ac69f">MV_CC_GetIntValue</a>(handle, <span class="stringliteral">&quot;PayloadSize&quot;</span>, &amp;stParam);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Get PayloadSize fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">        g_nPayloadSize = stParam.<a name="a22"></a><a class="code" href="struct_m_v_c_c___i_n_t_v_a_l_u_e.html#a83b19189b7bee89a25ea1f31e96e46e5">nCurValue</a>;</div><div class="line"></div><div class="line">        <span class="comment">// ch:开始取流 | en:Start grab image</span></div><div class="line">        nRet = <a name="a23"></a><a class="code" href="group___xE5_x9B_xBE_xE5_x83_x8F_xE9_x87_x87_xE9_x9B_x86.html#ga78728211c47c6f118110fde635d6ac69">MV_CC_StartGrabbing</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Start Grabbing fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        pthread_t nThreadID;</div><div class="line">        nRet = pthread_create(&amp;nThreadID, NULL ,WorkThread , handle);</div><div class="line">        <span class="keywordflow">if</span> (nRet != 0)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;thread create failed.ret = %d\n&quot;</span>,nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        PressEnterToExit();  </div><div class="line"></div><div class="line">        <span class="comment">// ch:停止取流 | en:Stop grab image</span></div><div class="line">        nRet = <a name="a24"></a><a class="code" href="group___xE5_x9B_xBE_xE5_x83_x8F_xE9_x87_x87_xE9_x9B_x86.html#gae5d7f4ae46a53851f104e26fa449ab06">MV_CC_StopGrabbing</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Stop Grabbing fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:关闭设备 | en:Close device</span></div><div class="line">        nRet = <a name="a25"></a><a class="code" href="group___xE7_x9B_xB8_xE6_x9C_xBA_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#ga33853bd328f46492102246ca8b645d57">MV_CC_CloseDevice</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Close Device fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ch:销毁句柄 | en:Destroy handle</span></div><div class="line">        nRet = <a name="a26"></a><a class="code" href="group___xE7_x9B_xB8_xE6_x9C_xBA_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#ga0f74372a22e8a19fa7e21dad45070428">MV_CC_DestroyHandle</a>(handle);</div><div class="line">        <span class="keywordflow">if</span> (MV_OK != nRet)</div><div class="line">        {</div><div class="line">            printf(<span class="stringliteral">&quot;Destroy Handle fail! nRet [0x%x]\n&quot;</span>, nRet);</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">        handle = NULL;</div><div class="line">    } <span class="keywordflow">while</span> (0);</div><div class="line">  </div><div class="line">    <span class="keywordflow">if</span> (handle != NULL)</div><div class="line">    {</div><div class="line">        <a class="code" href="group___xE7_x9B_xB8_xE6_x9C_xBA_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#ga0f74372a22e8a19fa7e21dad45070428">MV_CC_DestroyHandle</a>(handle);</div><div class="line">        handle = NULL;</div><div class="line">    }</div><div class="line">    </div><div class="line"></div><div class="line">    <span class="comment">// ch:反初始化SDK | en:Finalize SDK</span></div><div class="line">    <a name="a27"></a><a class="code" href="group___s_d_k_xE5_x88_x9D_xE5_xA7_x8B_xE5_x8C_x96.html#gafa1820a0da8671fc6f77dc9a7dfa69d3">MV_CC_Finalize</a>();</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;exit.\n&quot;</span>);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<div class="footer" style="text-align: right;">
	<hr/>
	<small></small>
</div>
<script>
	// 当前选中的菜单
	var currentMenuItem = document.querySelector('.tablist > li.current > a')
	if (currentMenuItem) { // 子菜单情况
	  window.parent.postMessage({
		navHref: currentMenuItem.href
	  }, '*')
	} else {
	  window.parent.postMessage({
		url: window.location.href
	  }, '*')
	}
  </script>
</body>
</html>